﻿using Abp.Collections.Extensions;
using Abp.Configuration.Startup;
using Abp.MultiTenancy;
using Abp.Runtime;
using Abp.Runtime.Session;
using Microsoft.AspNetCore.Http;
using NetCore.Authentication;
using NetCore.Enums;
using System.Linq;

namespace NetCore
{
    public class NetCoreSession : ClaimsAbpSession, INetCoreSession
    {
        public IHttpContextAccessor HttpContextAccessor;
        public NetCoreSession(IPrincipalAccessor principalAccessor, IHttpContextAccessor httpContextAccessor,
            IMultiTenancyConfig multiTenancy, ITenantResolver tenantResolver, IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider) : base(principalAccessor, multiTenancy, tenantResolver, sessionOverrideScopeProvider)
        {
            HttpContextAccessor = httpContextAccessor;
        }


        #region Implementation of INetCoreSession

        private string _userRole;
        /// <summary>
        /// 自定义规则(角色)名
        /// </summary>
        public string UserRole
        {
            get
            {
                if (Debug)
                {
                    return _userRole;
                }
                if (HttpContextAccessor.HttpContext == null)
                {
                    return _userRole;
                }
                var userRoleClaim = PrincipalAccessor.Principal?.Claims
                    .FirstOrDefault(c => c.Type == ARchGlClaimTypes.UserRole);
                return userRoleClaim?.Value;
            }
            set => _userRole = value;
        }
        public bool Debug { get; set; }

        //private long? _userId;
        //public new long? UserId
        //{
        //    get
        //    {
        //        if (Debug)
        //        {
        //            return _userId;
        //        }
        //        //ClaimsAbpSession 的UserId
        //        return base.UserId;

        //        //自定义identityServer4 根据IdentityServer4的ARchGlClaimTypes配置类型获取
        //        //var userIdClaim = PrincipalAccessor.Principal?.Claims
        //        //    .FirstOrDefault(c => c.Type == ARchGlClaimTypes.UserId);
        //        //if (string.IsNullOrEmpty(userIdClaim?.Value))
        //        //{
        //        //    return null;
        //        //}
        //        //if (long.TryParse(userIdClaim.Value, out var id))
        //        //{
        //        //    return id;
        //        //}
        //        //else
        //        //{
        //        //    return null;
        //        //}
        //    }
        //    set => _userId = value;
        //}

        private int? _companyId;

        public int? CompanyId
        {
            get
            {
                if (Debug)
                {
                    return _companyId;
                }
                if (HttpContextAccessor.HttpContext == null)
                {
                    return _companyId;
                }
                var stringValues = HttpContextAccessor.HttpContext.Request.Headers.GetOrDefault(NetCoreConsts.HeaderCompanyId);
                if (string.IsNullOrEmpty(stringValues.ToString()))
                {
                    return null;
                }

                if (int.TryParse(stringValues.ToString(), out var id))
                {
                    return id;
                }
                return null;
            }
            set => _companyId = value;
        }

        private int? _projectId;

        public int? ProjectId
        {
            get
            {
                if (Debug)
                {
                    return _projectId;
                }
                if (HttpContextAccessor.HttpContext == null)
                {
                    return _projectId;
                }
                var stringValues = HttpContextAccessor.HttpContext.Request.Headers.GetOrDefault(NetCoreConsts.HeaderProjectId);
                if (string.IsNullOrEmpty(stringValues.ToString()))
                {
                    return null;
                }

                if (int.TryParse(stringValues.ToString(), out var id))
                {
                    return id;
                }

                return null;
            }
            set => _projectId = value;
        }
        private int? _organizationId;

        public int? OrganizationId
        {
            get
            {
                if (Debug)
                {
                    return _projectId;
                }
                if (HttpContextAccessor.HttpContext == null)
                {
                    return _projectId;
                }
                var stringValues = HttpContextAccessor.HttpContext.Request.Headers.GetOrDefault(NetCoreConsts.HeaderOrganizationId);
                if (string.IsNullOrEmpty(stringValues.ToString()))
                {
                    return null;
                }

                if (int.TryParse(stringValues.ToString(), out var id))
                {
                    return id;
                }

                return null;
            }
            set => _projectId = value;
        }


        #endregion
    }
}
